﻿<%@ Page Title="" Language="C#" ClientIDMode=Static MasterPageFile="~/User/User.Master" AutoEventWireup="true" CodeBehind="XSHT_Edit.aspx.cs" Inherits="CoolERP_Web.User.XSHT_Edit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script src="../Scripts/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
<script src="../Scripts/lhgdialog/lhgcore.min.js" type="text/javascript"></script>
<script src="../Scripts/lhgdialog/lhgdialog.js" type="text/javascript"></script>
<script type="text/javascript">
    var count = 1;
    var old_count = "";
    $(function () {
        $("[id='txtCount']").keydown(function (event) {
            old_count = $(this).val();
            var k = event.keyCode;
            if ((k <= 57 && k >= 48) || (k <= 105 && k >= 96) || (k == 8))
                return true;
            else {
                $(this).val(old_count);
                return false;
            }
        });
    });
    function setChange(obj) {
        var count = parseInt($(obj).val());
        if (count > 0) {
            var price = $(obj).closest("tr").find("[id=txtPrice]").val();
            if (isEmpty(price))
                price = 0;
            var total = $(obj).closest("tr").find("td").eq(6);
            total.text((count * price).toFixed(2));
            setTotal();
        }
        else {
            $(obj).val(old_count);
            alert('数量不能小于1');
        }
    }
    function setTotal() {
        var totalMoney = 0;
        $("#tbGoodsInfo tr").each(function (i) {
            if (i > 0) {
                var txt_price = $(this).find("[id='txtPrice']").val();
                var txt_count = $(this).find("[id='txtCount']").val();
                if (!isEmpty(txt_price) && !isEmpty(txt_count))
                    totalMoney += parseFloat(txt_price) * parseFloat(txt_count);
            }
        });
        $("#spanMoney").text(totalMoney.toFixed(2));
        $("#divTotalMoney").show();
    }
    function resetXH() {
        $("#tbGoodsInfo tr").each(function (i) {
            if (i > 0)
                $(this).find("td").eq(0).text(i);
        });
    }
    function setHid() {
        var ids = "";
        $("#tbGoodsInfo input[type=hidden]").each(function () {
            ids += $(this).attr("value") + ",";
        });
        if (ids != "")
            ids = ids.substr(0, ids.length - 1);
        $("#hidSelIds").val(ids);
    }
    function DelGoods(obj) {
        $(obj).parent().parent().remove();
        resetXH();
        setHid();
        setTotal();
    }
    var dg;
    function selGoods() {
        dg = new J.dialog({ id: 'select', title: '选择产品', page: 'SelectCP.aspx?ids=' + $("#hidSelIds").val(), iconTitle: false, cover: true, btnBar: false, rang: true, autoSize: true, width: 900, height: 650 });
        dg.ShowDialog();
    }
    function selCustomer() {
        dg = new J.dialog({ id: 'select', title: '选择客户', page: 'SelectCustomer.aspx', iconTitle: false, cover: true, btnBar: false, rang: true, autoSize: true, width: 900, height: 650 });
        dg.ShowDialog();
    }
    function selMore(goodsList) {
        resetXH();
        for (var i = 0; i < goodsList.length; i++) {
            var row = $("#tbGoodsInfo tr").length;
            var name = goodsList[i].name;
            var id = goodsList[i].id;
            var gg = goodsList[i].gg;
            var ys = goodsList[i].ys;
            var price = goodsList[i].price;

            $("<tr><td>" + row + "</td><td>" + name + "<input type=hidden id='hidGid' value='" + id + "' /></td><td>" + gg + "</td><td>" + ys + "</td>\
                <td><input type=text class='textbox' id='txtPrice' value='" + price + "' style='width:60px; text-align:center' /></td>\
                <td><input type=text class='textbox' id='txtCount' value='' style='width:60px; text-align:center' onchange='setChange(this)' /></td><td></td>\
                <td><img src='../Images/Common/btn_del0.png' onclick='DelGoods(this)' style='cursor:pointer' /></td></tr>").appendTo("#tbGoodsInfo");
        }
        setHid();
        dg.cancel();
    }    
    function checkSubmit() {
        if (isEmpty($("#hidKHID").val())) {
            alert("请选择客户");
            return false;
        }
        if (isEmpty($("#ddlDepartments").val())) {
            alert("请选择销售部门");
            return false;
        }
        if (isEmpty($("#hidStaff").val())) {
            alert("请选择销售人员");
            return false;
        }
        if (isEmpty($("#txtDHSJ").val())) {
            alert("请选择订货时间");
            return false;
        }
        if (isEmpty($("#txtCHSJ").val())) {
            alert("请选择出货时间");
            return false;
        }
        var isOk = true;
        var idList = new Array();
        var priceList = new Array();
        var countList = new Array();
        $("#tbGoodsInfo tr").each(function (i, item) {
            if (i > 0) {
                var goods = {
                    id: $(this).find("input[type=hidden]").val(),
                    name: $(this).find("td").eq(1).val(),
                    price: $(this).find("input[type=text]").eq(0).val(),
                    count: $(this).find("input[type=text]").eq(1).val()
                };
                if (!isEmpty(goods.id) && isEmpty(goods.count)) {
                    alert("请填写第" + i + "个产品的数量");
                    isOk = false;
                    return isOk;
                }
                if (idList.contains(goods.id)) {
                    alert("有两个相同的产品信息：" + goods.name);
                    isOk = false;
                    return isOk;
                }
                idList[idList.length] = goods.id;
                priceList[priceList.length] = goods.price;
                countList[countList.length] = goods.count;
            }
        });
        if (!isOk)
            return false;
        $("#hidCPID").val(idList);
        $("#hidPrices").val(priceList);
        $("#hidCounts").val(countList);
        if (isEmpty($("#hidCPID").val())) {
            alert("至少必须添加一件产品明细");
            return false;
        }
        return true;
    }
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td class=tbHead_L></td>
        <td class=tbHead_M><div><b><asp:Literal ID="liTitle" runat=server>新增</asp:Literal>销售合同</b></div></td>
        <td class=tbHead_R></td>
    </tr>
    <tr>
        <td class=tbBody_L></td>
        <td class=tbBody_M>
        <div style="width:100%;">
            <ul class=tags style="width:100%;">
                <li class=liTitle><div>合同详细信息</div></li>
            </ul>
            <table class="grvCSS" cellspacing="0" cellpadding=2 border="1" style="width:100%;border-collapse:collapse;">
                <tr>
                    <td width=10%>客户简称：</td>
                    <td style="text-align:left;width:30%"><asp:TextBox ID="txtKH" runat="server" CssClass="textbox" MaxLength="16" ReadOnly=true></asp:TextBox>
                        <input type=button value="选择客户" class=btn_s onclick="selCustomer()" />
                        <asp:HiddenField ID="hidKHID" runat="server" /></td>
                    <td width=10%>出货方式：</td>
                    <td style="text-align:left;width:20%"><asp:DropDownList ID="ddlCHFS" runat="server" CssClass="dropdown" Width="150px">
                    <asp:ListItem>自提</asp:ListItem><asp:ListItem>代办运输</asp:ListItem></asp:DropDownList></td>                                            
                    <td width=10%>合同状态：</td>
                    <td style="text-align:left;width:20%"><asp:DropDownList ID="ddlhtzt" runat="server" CssClass="dropdown" Width="150px">
                            <asp:ListItem Value="0">未完成</asp:ListItem>
                            <asp:ListItem Value="1">完成</asp:ListItem>
                        </asp:DropDownList></td>
                </tr>
                <tr>
                    <td width=10%>销售部门：</td>
                    <td style="text-align:left; width:60%" colspan=3><asp:DropDownList ID="ddlDepartments" runat="server" CssClass="dropdown" onchange="BindStaff('ddlSaler',this.value,'请选择')"/></td>
                    <td width=10%>销售人员：</td>
                    <td style="text-align:left;width:20%"><asp:DropDownList ID="ddlSaler" runat="server" CssClass="dropdown" Width="150px" onchange="setStaff('hidStaff',this.value)"/>
                    <input type="hidden" runat=server id="hidStaff" /></td>
                </tr>
                <tr>
                    <td width=10%>订货时间：</td>
                    <td style="text-align:left;width:30%"><asp:TextBox ID="txtDHSJ" runat="server" CssClass="textbox Wdate" MaxLength="16" onclick="WdatePicker({dateFmt: 'yyyy-MM-dd HH:mm'});"></asp:TextBox></td>
                    <td width=10%>出货时间：</td>
                    <td style="text-align:left"><asp:TextBox ID="txtCHSJ" runat="server" CssClass="textbox Wdate" MaxLength="16" onclick="WdatePicker({dateFmt: 'yyyy-MM-dd HH:mm'});"></asp:TextBox></td>                                            
                    <td colspan=2 width=30%></td>
                </tr>
            </table>
            <ul class=tags style="width:100%;">
                <li class=liTitle><div>订单明细列表</div></li>
                <li class=liRight><img onclick="selGoods()" title="增加产品明细" src="../Images/Common/btn_add0.png" style="cursor:pointer" /></li>
            </ul>
            <table id="tbGoodsInfo" class=grvCSS cellpadding=0 cellspacing=0 border=1 style="width:100%;border-collapse:collapse;">
                <tr>
                    <th>序号</th><th>产品名称</th><th>产品规格</th><th>颜色</th><th>单价</th><th>数量</th><th>金额</th><th width=100px></th>
                </tr>            
            <asp:Repeater ID="repMXList" runat="server">
            <ItemTemplate>
                <tr>
                    <td><%# Container.ItemIndex + 1%></td>
                    <td><%# Eval("CPMC") %><input type=hidden id="hidGid" value='<%# Eval("CPID") %>' /></td>
                    <td><%# Eval("CPGG") %></td><td><%# Eval("CPYS") %></td>
                    <td><input type=text class="textbox" id="txtPrice" value='<%# Eval("DJ") %>' style="width:60px; text-align:center" /></td>
                    <td><input type=text class="textbox" id="txtCount" value='<%# Eval("SL") %>' style="width:60px; text-align:center" onchange="setChange(this)" /></td>
                    <td><%# (Convert.ToDecimal(Eval("DJ")) * Convert.ToDecimal(Eval("SL"))).ToString("C")%></td>
                    <td><img src="../Images/Common/btn_del0.png" onclick="DelGoods(this)" style="cursor:pointer" /></td>
                </tr>
            </ItemTemplate>
            </asp:Repeater>
            </table>
            <asp:Panel ID="divTotalMoney" runat="server">
                总计金额：¥<asp:Label ID="spanMoney" runat="server" ForeColor=Red/>
            </asp:Panel>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <p><asp:ImageButton ID="btnSave" runat="server" ImageUrl="~/Images/Common/btn_save0.png" onclick="btnSave_Click" OnClientClick="return checkSubmit();" />&nbsp;&nbsp;
                <a href="XSHT_Query.aspx"><img src="../Images/Common/btn_back0.png" border=0 /></a>&nbsp;&nbsp;
                <asp:Label ID="labResult" runat="server" ForeColor=Red></asp:Label>
            </p></ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
            </Triggers>
            </asp:UpdatePanel>
        </div>
        </td>
        <td class=tbBody_R></td>
    </tr>
    <tr>
        <td class=tbFoot_L></td>
        <td class=tbFoot_M></td>
        <td class=tbFoot_R></td>
    </tr>
</table>
<asp:HiddenField ID="hidCPID" runat="server" /><asp:HiddenField ID="hidPrices" runat="server" />
<asp:HiddenField ID="hidCounts" runat="server" />
</asp:Content>